﻿using MES.Server.Common;
using MES.Server.Entities;
using MES.Server.IService;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace MES.Server.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class MaterialController : ControllerBase
    {
        IMaterialService _materialService;
        public MaterialController(IMaterialService materialService)
        {
            _materialService = materialService;
        }

        #region 物料分类
        [HttpGet("class/{key}")]
        [Authorize]
        public async Task<JsonResult> GetClassAll([FromRoute] string key)
        {
            ApiDataResult<MaterialClassEntity[]> result = new ApiDataResult<MaterialClassEntity[]>();
            try
            {
                key = key == "none" ? "" : key;
                var ms = _materialService.GetClassAll(key);
                result.Data = ms.ToArray();
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("class/update")]
        [Authorize]
        public async Task<JsonResult> UpdateMaterialClass(MaterialClassEntity entity)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _materialService.UpdateClass(entity);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpPost("class/delete/ids")]
        [Authorize]
        public async Task<JsonResult> DeleteMaterialClass(int[] ids)
        {
            ApiDataResult<int> result = new ApiDataResult<int>();
            try
            {
                var count = _materialService.DeleteClass(ids);
                result.Data = count;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }

        [HttpGet("class/check/{id}/{name}")]
        [Authorize]
        public async Task<JsonResult> CheckDepartName(string name, int id)
        {
            ApiDataResult<bool> result = new ApiDataResult<bool>();
            try
            {
                var state = _materialService.CheckClassName(name, id);
                result.Data = state;
            }
            catch (Exception ex)
            {
                result.Success = false;
                result.OValue = 500;
                result.Message = ex.Message;
            }
            return await Task.FromResult(new JsonResult(result));
        }
        #endregion
    }
}
